(function(){
    var balance;
    var index = 0;
    /**
    * 初始化
    */
    async function init(){
        if (!checkConnect()) return;
        var rtn = await loadManbers();
        if (rtn){
            //设置转账列表
            setList("toObject");
            var list = getNode("toObject");
            var line = list.children[0];
            //因为允许转到公库，所以手动添加2条
            var newLine = line.cloneNode(true);
            newLine.children[0].value = current.TREASURY;
            newLine.children[1].innerText = "DAO组织公库";
            //插在最前面
            list.insertBefore(newLine,line);
            newLine = line.cloneNode(true);
            newLine.children[0].value = current.EXCHANGE;
            newLine.children[1].innerText = "内置交易所";
            //插在最前面
            list.insertBefore(newLine,line);
            //可以选择捐赠不同的代币
            getNode("selectToken").onchange = changeToken;
            //显示选定的余额
            changeToken();
        }
	}
    /**
    * 列表切换后显示不同代币的余额
    */
    async function changeToken(){
        index = this.selectedIndex;
        if(index == undefined) index=0;
        if (index==0) {
            balance = await HANbalance();
            setNodeText("balanceOf",format(balance));
        }else if (index==1){
            balance = await DAIbalance();
            setNodeText("balanceOf",format(balance));
        }else{
            balance = await MATICbalance();
            setNodeText("balanceOf",format(balance));
        } 
        //不知为什么直接用局域变量在下面会说未定义
        publicParam.balance = balance;
    }
    /**
    * 点击捐赠后的业务逻辑
    */
    getNode("transfer").onclick=async function(){
        if (!checkConnect()) return;
        var amount = parse(getNode("amount").value);
        if (amount<=0) {
            alert("金额不能小于0");
            return;
        }
        if (amount.gt(publicParam.balance)){
            alert("您的余额不足");
            return;
        }
        var toObject = getList("toObject");
        if (toObject.length==0){
            alert("没有选择捐赠对象");
            return;
        }
        if (toObject[0]==publicParam.address){
            alert("转给自己，这样只会白白浪费MATIC");
            return;
        }
        setLoading(1);
        if (index==2){
            const rtn = await MATICtransfer("转账"+getNode("amount").value+"个MATIC",toObject[0],amount);
            if (rtn){
                //因为还花了手续费，所以要重新取一下余额
                var balance = await MATICbalance();
                var matic = format(balance);
                setNodeText("balanceOf",matic);
                getNode("amount").value=0;
                clearList("toObject");
            }
        }else{
            var rtn;
            var txt = "转账"+getNode("amount").value;
            if (index==0){
                txt += "个HAN";
                rtn = await HANtransfer(txt,toObject[0],amount);
            }else if (index==1){
                txt += "个DAI";
                rtn = await DAItransfer(txt,toObject[0],amount);
            }
            if(rtn){
                if (index==0) balance = await HANbalance();
                else balance = await DAIbalance();
                setNodeText("balanceOf",format(balance));
                getNode("amount").value=0;
                clearList("toObject");
            }
        }
        setLoading(0);
    };
	init();
})();